{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Python 使用API模板"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "metadata": {},
   "outputs": [],
   "source": [
    "import requests"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 52,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 1. URL\n",
    "base_url = 'https://api-cn.faceplusplus.com/facepp/v3/detect'\n",
    "\n",
    "\n",
    "# 2. api账号/通行证\n",
    "API_key = 'JAGR_NQMxTZWeRCq_7UV6QC8Eq2uSe-p'\n",
    "API_secret = '8c0DvbbGq8DZesE9peYVPjrskuvI5y36'\n",
    "\n",
    "\n",
    "# 3. 请求方法：POST\n",
    "\n",
    "\n",
    "# 4. 准备payload（酬载）\n",
    "payload = {\n",
    "    'api_key':API_key,\n",
    "    'api_secret':API_secret,\n",
    "    'image_url':'https://tse1-mm.cn.bing.net/th/id/OIP-C.HPWFQ7UDQh93EKcDmiUQ_wHaIR?w=176&h=197&c=7&r=0&o=5&dpr=2&pid=1.7',\n",
    "    'return_attributes':'gender,age,smiling,headpose,facequality,blur,eyestatus,emotion,beauty,mouthstatus,eyegaze,skinstatus,nose_occlusion,chin_occlusion,face_occlusion'\n",
    "}\n",
    "\n",
    "\n",
    "# 5. 发起请求\n",
    "r = requests.post(url = base_url, params = payload)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 53,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "<Response [200]>"
      ]
     },
     "execution_count": 53,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "r"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 54,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "b'{\"request_id\":\"1647866357,f844a203-e29e-4f76-a7c4-a38f66b839e0\",\"time_used\":507,\"faces\":[{\"face_token\":\"1a299c42bce264c2bd376d45106bcbe1\",\"face_rectangle\":{\"top\":108,\"left\":141,\"width\":107,\"height\":107},\"attributes\":{\"gender\":{\"value\":\"Male\"},\"age\":{\"value\":53},\"smile\":{\"value\":100.000,\"threshold\":50.000},\"headpose\":{\"pitch_angle\":16.763775,\"roll_angle\":2.3050764,\"yaw_angle\":0.004189467},\"blur\":{\"blurness\":{\"value\":0.364,\"threshold\":50.000},\"motionblur\":{\"value\":0.364,\"threshold\":50.000},\"gaussianblur\":{\"value\":0.364,\"threshold\":50.000}},\"eyestatus\":{\"left_eye_status\":{\"no_glass_eye_open\":99.998,\"no_glass_eye_close\":0.000,\"normal_glass_eye_open\":0.002,\"normal_glass_eye_close\":0.000,\"dark_glasses\":0.000,\"occlusion\":0.000},\"right_eye_status\":{\"no_glass_eye_open\":99.961,\"no_glass_eye_close\":0.000,\"normal_glass_eye_open\":0.031,\"normal_glass_eye_close\":0.000,\"dark_glasses\":0.000,\"occlusion\":0.008}},\"emotion\":{\"anger\":0.007,\"disgust\":1.213,\"fear\":0.112,\"happiness\":97.970,\"neutral\":0.007,\"sadness\":0.681,\"surprise\":0.009},\"facequality\":{\"value\":85.658,\"threshold\":70.100},\"beauty\":{\"male_score\":56.694,\"female_score\":56.631},\"mouthstatus\":{\"surgical_mask_or_respirator\":0.000,\"other_occlusion\":0.000,\"close\":0.000,\"open\":100.000},\"eyegaze\":{\"left_eye_gaze\":{\"position_x_coordinate\":0.506,\"position_y_coordinate\":0.436,\"vector_x_component\":0.168,\"vector_y_component\":0.269,\"vector_z_component\":0.948},\"right_eye_gaze\":{\"position_x_coordinate\":0.468,\"position_y_coordinate\":0.443,\"vector_x_component\":-0.054,\"vector_y_component\":0.325,\"vector_z_component\":0.944}},\"skinstatus\":{\"health\":9.525,\"stain\":12.624,\"dark_circle\":1.982,\"acne\":0.634},\"glass\":{\"value\":\"None\"},\"nose_occlusion\":{\"nose_occ\":0},\"chin_occlusion\":{\"chin_occ\":0},\"face_occlusion\":{\"face_left_occlusion\":0,\"face_right_occlusion\":0}}}],\"image_id\":\"JoYdaZ0h1hGb4ih1d2EZVw==\",\"face_num\":1}\\n'"
      ]
     },
     "execution_count": 54,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "r.content"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 55,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'request_id': '1647866357,f844a203-e29e-4f76-a7c4-a38f66b839e0',\n",
       " 'time_used': 507,\n",
       " 'faces': [{'face_token': '1a299c42bce264c2bd376d45106bcbe1',\n",
       "   'face_rectangle': {'top': 108, 'left': 141, 'width': 107, 'height': 107},\n",
       "   'attributes': {'gender': {'value': 'Male'},\n",
       "    'age': {'value': 53},\n",
       "    'smile': {'value': 100.0, 'threshold': 50.0},\n",
       "    'headpose': {'pitch_angle': 16.763775,\n",
       "     'roll_angle': 2.3050764,\n",
       "     'yaw_angle': 0.004189467},\n",
       "    'blur': {'blurness': {'value': 0.364, 'threshold': 50.0},\n",
       "     'motionblur': {'value': 0.364, 'threshold': 50.0},\n",
       "     'gaussianblur': {'value': 0.364, 'threshold': 50.0}},\n",
       "    'eyestatus': {'left_eye_status': {'no_glass_eye_open': 99.998,\n",
       "      'no_glass_eye_close': 0.0,\n",
       "      'normal_glass_eye_open': 0.002,\n",
       "      'normal_glass_eye_close': 0.0,\n",
       "      'dark_glasses': 0.0,\n",
       "      'occlusion': 0.0},\n",
       "     'right_eye_status': {'no_glass_eye_open': 99.961,\n",
       "      'no_glass_eye_close': 0.0,\n",
       "      'normal_glass_eye_open': 0.031,\n",
       "      'normal_glass_eye_close': 0.0,\n",
       "      'dark_glasses': 0.0,\n",
       "      'occlusion': 0.008}},\n",
       "    'emotion': {'anger': 0.007,\n",
       "     'disgust': 1.213,\n",
       "     'fear': 0.112,\n",
       "     'happiness': 97.97,\n",
       "     'neutral': 0.007,\n",
       "     'sadness': 0.681,\n",
       "     'surprise': 0.009},\n",
       "    'facequality': {'value': 85.658, 'threshold': 70.1},\n",
       "    'beauty': {'male_score': 56.694, 'female_score': 56.631},\n",
       "    'mouthstatus': {'surgical_mask_or_respirator': 0.0,\n",
       "     'other_occlusion': 0.0,\n",
       "     'close': 0.0,\n",
       "     'open': 100.0},\n",
       "    'eyegaze': {'left_eye_gaze': {'position_x_coordinate': 0.506,\n",
       "      'position_y_coordinate': 0.436,\n",
       "      'vector_x_component': 0.168,\n",
       "      'vector_y_component': 0.269,\n",
       "      'vector_z_component': 0.948},\n",
       "     'right_eye_gaze': {'position_x_coordinate': 0.468,\n",
       "      'position_y_coordinate': 0.443,\n",
       "      'vector_x_component': -0.054,\n",
       "      'vector_y_component': 0.325,\n",
       "      'vector_z_component': 0.944}},\n",
       "    'skinstatus': {'health': 9.525,\n",
       "     'stain': 12.624,\n",
       "     'dark_circle': 1.982,\n",
       "     'acne': 0.634},\n",
       "    'glass': {'value': 'None'},\n",
       "    'nose_occlusion': {'nose_occ': 0},\n",
       "    'chin_occlusion': {'chin_occ': 0},\n",
       "    'face_occlusion': {'face_left_occlusion': 0, 'face_right_occlusion': 0}}}],\n",
       " 'image_id': 'JoYdaZ0h1hGb4ih1d2EZVw==',\n",
       " 'face_num': 1}"
      ]
     },
     "execution_count": 55,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "r.json()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 56,
   "metadata": {},
   "outputs": [],
   "source": [
    "face_detect = r.json()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 57,
   "metadata": {},
   "outputs": [],
   "source": [
    "face_tokens = face_detect['faces'][0]['face_token']"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 58,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'1a299c42bce264c2bd376d45106bcbe1'"
      ]
     },
     "execution_count": 58,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "face_tokens"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 人脸分析"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 61,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 1. URL\n",
    "Analyze_base_url = 'https://api-cn.faceplusplus.com/facepp/v3/face/analyze'\n",
    "\n",
    "\n",
    "# 2. api账号/通行证\n",
    "API_key = 'JAGR_NQMxTZWeRCq_7UV6QC8Eq2uSe-p'\n",
    "API_secret = '8c0DvbbGq8DZesE9peYVPjrskuvI5y36'\n",
    "\n",
    "\n",
    "# 3. 请求方法：POST\n",
    "\n",
    "\n",
    "# 4. 准备payload（酬载）\n",
    "payload = {\n",
    "    'api_key':API_key,\n",
    "    'api_secret':API_secret,\n",
    "    'face_tokens':face_tokens,\n",
    "    'image_url':'https://tse1-mm.cn.bing.net/th/id/OIP-C.HPWFQ7UDQh93EKcDmiUQ_wHaIR?w=176&h=197&c=7&r=0&o=5&dpr=2&pid=1.7',\n",
    "    'return_attributes':'gender,age,smiling,headpose,facequality,blur,eyestatus,emotion,beauty,mouthstatus,eyegaze,skinstatus,nose_occlusion,chin_occlusion,face_occlusion'\n",
    "}\n",
    "\n",
    "\n",
    "# 5. 发起请求\n",
    "r_analyze = requests.post(url = Analyze_base_url, params = payload)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 63,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "<Response [400]>"
      ]
     },
     "execution_count": 63,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "r_analyze"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 人脸比对\n",
    "* 思考"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 1. URL\n",
    "Analyze_base_url = https://api-cn.faceplusplus.com/facepp/v3/compare'\n",
    "\n",
    "\n",
    "# 2. api账号/通行证\n",
    "API_key = 'JAGR_NQMxTZWeRCq_7UV6QC8Eq2uSe-p'\n",
    "API_secret = '8c0DvbbGq8DZesE9peYVPjrskuvI5y36'\n",
    "\n",
    "\n",
    "# 3. 请求方法：POST\n",
    "\n",
    "\n",
    "# 4. 准备payload（酬载）\n",
    "payload = {\n",
    "    'api_key':API_key,\n",
    "    'api_secret':API_secret,\n",
    "    'face_tokens':face_tokens,\n",
    "    'image_url':'https://tse1-mm.cn.bing.net/th/id/OIP-C.HPWFQ7UDQh93EKcDmiUQ_wHaIR?w=176&h=197&c=7&r=0&o=5&dpr=2&pid=1.7',\n",
    "    'return_attributes':'gender,age,smiling,headpose,facequality,blur,eyestatus,emotion,beauty,mouthstatus,eyegaze,skinstatus,nose_occlusion,chin_occlusion,face_occlusion'\n",
    "}\n",
    "\n",
    "\n",
    "# 5. 发起请求\n",
    "r_analyze = requests.post(url = Analyze_base_url, params = payload)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 封装人脸检测函数，实现快速调用"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "* 准备工作"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "API_key = 'JAGR_NQMxTZWeRCq_7UV6QC8Eq2uSe-p'\n",
    "API_secret = '8c0DvbbGq8DZesE9peYVPjrskuvI5y36'\n",
    "image_url_01:'https://tse1-mm.cn.bing.net/th/id/OIP-C.HPWFQ7UDQh93EKcDmiUQ_wHaIR?w=176&h=197&c=7&r=0&o=5&dpr=2&pid=1.7',\n",
    "image_url_02:'https://gimg2.baidu.com/image_search/src=http%3A%2F%2Fup.enterdesk.com%2Fedpic_source%2Fd4%2F0d%2F8c%2Fd40d8c1fe6b371029baae225f9c2b008.jpg&refer=http%3A%2F%2Fup.enterdesk.com&app=2002&size=f9999,10000&q=a80&n=0&g=0n&fmt=auto?sec=1650459478&t=95a7c40e2ad54c61e17ac8a62a198a55'"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "def detect_face\n",
    "base_url = 'https://api-cn.faceplusplus.com/facepp/v3/detect'\n",
    "API_key = 'JAGR_NQMxTZWeRCq_7UV6QC8Eq2uSe-p'\n",
    "API_secret = '8c0DvbbGq8DZesE9peYVPjrskuvI5y36'\n",
    "\n",
    "payload = {\n",
    "    'api_key':API_key,\n",
    "    'api_secret':API_secret,\n",
    "\n",
    "    'return_attributes':'gender,age,smiling,headpose,facequality,blur,eyestatus,emotion,beauty,mouthstatus,eyegaze,skinstatus,nose_occlusion,chin_occlusion,face_occlusion'\n",
    "}\n",
    "\n",
    "r = requests.post(url = base_url, params = payload)"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.0"
  },
  "toc": {
   "base_numbering": 1,
   "nav_menu": {},
   "number_sections": true,
   "sideBar": true,
   "skip_h1_title": false,
   "title_cell": "Table of Contents",
   "title_sidebar": "Contents",
   "toc_cell": false,
   "toc_position": {},
   "toc_section_display": true,
   "toc_window_display": false
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
